package br.com.marcio.obj;

import java.util.Vector;

public class Mesh {

    private Vector<Group> groups;
    private Vector<Vertex> vertexes;
    private Vector<Vertex> normals;
//    private Vector<TextureMapping> texturesMapping;
//    private Vector<Material> materials;
    private Group grupoAtual;

    public Mesh() {
        this.groups = new Vector<Group>();
        this.vertexes = new Vector<Vertex>();
        this.normals = new Vector<Vertex>();
//        this.texturesMapping = new Vector<TextureMapping>();
//        this.materials = new Vector<Material>();
    }

    public void addGroup(Group g) {
        for (Group group : groups) {
            String name = group.getName();
            if(name.equals(g.getName())) {
                setGrupoAtual(group);
                return;
            }
        }
        groups.add(g);
        setGrupoAtual(g);
    }

    public void addVertex(Vertex v) {
        vertexes.add(v);
    }

    public void addNormal(Vertex v) {
        normals.add(v);
    }

//    public void addTextureMapping(TextureMapping t) {
//        texturesMapping.add(t);
//    }

//    public void addMaterial(Material m) {
//        materials.add(m);
//    }

    public Group getGrupoAtual() {
        return grupoAtual;
    }

    public void setGrupoAtual(Group grupoAtual) {
        this.grupoAtual = grupoAtual;
    }

    public Vertex getVertex(int i) {
        return vertexes.get(i);
    }

    public Vertex getNormal(int i) {
        try {
            return normals.get(i);
        } catch(ArrayIndexOutOfBoundsException e) {
            return null;
        }
    }

//    public TextureMapping getTextureMapping(int i) {
//        try {
//            return texturesMapping.get(i);
//        } catch(ArrayIndexOutOfBoundsException e) {
//            return null;
//        }
//    }

    public Vector<Group> getGrupos() {
        return groups;
    }

    public Group getGrupoByIndex(int index) {
        for (Group group : groups) {
            int groupIndex = group.getIndex();

            if(groupIndex == index) {
                return group;
            }
        }
        return null;
    }

    public Group getSelectedGroup() {
        for (Group group : groups) {
            if(group.isSelected()) {
                return group;
            }
        }
        return null;
    }

    public void selectGroup(int index) {
        for (Group group : groups) {
            if(group.isSelected()) {
                group.deselect();
                break;
            }
        }
        getGrupoByIndex(index).select();
    }

//    public Material getMaterial(String materialName) {
//        for (Material material : materials) {
//            String name = material.getName();
//            if(name.equals(materialName)) {
//              return material;
//            }
//        }
//        return null;
//    }

//    public void addFace(int[] vertexes, int[] normals, int[] textureMappings, String materialName) {
//        Face f = new Face();
//
//        for (int i = 0; i < vertexes.length; i++) {
//            int vi = vertexes[i] - 1;
//
//            f.addVertex(getVertex(vi));
//			
//            if (normals[i] - 1 != -1) {
//                f.addNormal(getNormal(normals[i] -1));
//            }
//			
//            if (textureMappings[i] - 1 != -1) {
//                f.addTextureMapping(getTextureMapping(textureMappings[i] - 1));
//            }
//        }
//
//        if (!materialName.equals("")) {
//            f.setMaterialName(materialName);
//        }
//
//        getGrupoAtual().addFace(f);
//    }
    
    public void addFace(int[] vertexes, int[] normals) {
        Face f = new Face();

        for (int i = 0; i < vertexes.length; i++) {
            int vi = vertexes[i] - 1;

            f.addVertex(getVertex(vi));
			
            if (normals[i] - 1 != -1) {
                f.addNormal(getNormal(normals[i] -1));
            }
			
        }

        getGrupoAtual().addFace(f);
    }

    public void addFace(int[][] matrix, String materialName) {
        addFace(matrix[0], matrix[2]);
    }
}
